package com.cloudsigns.factorymanager2.project.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tony on 2018/3/10.
 */

public class AddEngineeringInstallationOrderModel1 implements IAddEngineeringInstallationOrderModel1 {

    @Override
    public void addEngineeringInatallationOrder1(final long completeDate,
                                                 final String engineeringAddress,
                                                 final String engineeringName,
                                                 final double lat,
                                                 final double lon,
                                                 final String picUrl,
                                                 final String projectName,
                                                 final List<ProductBean> productBeans,
                                                 final OnAddEngineeringInstallationOrderListener1 onAddEngineeringInstallationOrderListener) {

        new Thread(new Runnable() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.ADD_ENGINEERING_INSTALLATION_ORDER_URL);

                HttpResponse response;
                try {
                    // 添加http头信息
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("completeDate", completeDate);
                    jsonObject.put("engineeringAddress", engineeringAddress);
                    jsonObject.put("engineeringName", engineeringName);
                    jsonObject.put("lat", lat);
                    jsonObject.put("lon", lon);
                    jsonObject.put("picUrl", picUrl);

                    if (null != productBeans && productBeans.size() > 0) {
                        JSONArray products = new JSONArray();

                        for (int i = 0; i < productBeans.size(); i++) {
                            ProductBean productBean = productBeans.get(i);
                            JSONObject productObj = new JSONObject();
                            productObj.put("amount", productBean.getAmount());
                            productObj.put("id", productBean.getId());
                            productObj.put("name", productBean.getName());
                            productObj.put("picurl", productBean.getPicUrl1());

                            JSONObject shapeTypeObj = new JSONObject();
                            shapeTypeObj.put("border", productBean.getBorder());
                            shapeTypeObj.put("borderBottom", productBean.getBorderBottom());
                            shapeTypeObj.put("borderLeft", productBean.getBorderLeft());
                            shapeTypeObj.put("borderRight", productBean.getBorderRight());
                            shapeTypeObj.put("borderTop", productBean.getBorderTop());
                            shapeTypeObj.put("desc", productBean.getDesc());
                            shapeTypeObj.put("diameter", productBean.getDiameter());
                            shapeTypeObj.put("height", productBean.getHeight());
                            shapeTypeObj.put("thick", productBean.getThick());
                            if (null != productBean.getType()) {
                                CommonEnum.ProductShapeType type = productBean.getType();
                                if (type == CommonEnum.ProductShapeType.RECTANGLE) {
                                    shapeTypeObj.put("type", "RECTANGLE");
                                } else if (type == CommonEnum.ProductShapeType.ROUND) {
                                    shapeTypeObj.put("type", "ROUND");

                                } else if (type == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                                    shapeTypeObj.put("type", "SPECIALSHAPE");

                                }
                            }
                            shapeTypeObj.put("width", productBean.getWidth());
                            productObj.put("shapeType", shapeTypeObj);


                            productObj.put("unit", productBean.getUnit());

                            products.put(productObj);

                        }

                        jsonObject.put("productList", products);

                    } else {
                        jsonObject.put("productList", new JSONArray());
                    }

                    jsonObject.put("projectName", projectName);

                    Log.e("request",jsonObject.toString().trim());


                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);

                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());

                    httpPost.setEntity(se);
                    Log.e("request=","token" + MyApplicaton.getApplication().getToken());
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    Log.e("response=",json.toString().trim());
                    String code = json.getString("code");
                    if (null != onAddEngineeringInstallationOrderListener) {

                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {

                            onAddEngineeringInstallationOrderListener.success(json.getString("id"));
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            onAddEngineeringInstallationOrderListener.failed(error_str);


                        }

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }



    public interface OnAddEngineeringInstallationOrderListener1 {
        void success(String projectId);

        void failed(String desc);
    }

}
